//
// DotNetNuke - http://www.dotnetnuke.com
// Copyright (c) 2002-2010
// by Perpetual Motion Interactive Systems Inc. ( http://www.perpetualmotion.ca )
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
// documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
// the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
// to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions 
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
// CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
// DEALINGS IN THE SOFTWARE.
//

using System;
using System.IO;
using DotNetNuke.Common.Utilities;
using DotNetNuke.Entities.Modules;
using DotNetNuke.Entities.Modules.Actions;
using DotNetNuke.Entities.Modules.Definitions;
using DotNetNuke.Services.Installer.Packages;
namespace DotNetNuke.UI.UserControls
{
	public abstract class Help : DotNetNuke.Entities.Modules.PortalModuleBase
	{
		protected System.Web.UI.WebControls.Label lblHelp;
		protected System.Web.UI.WebControls.LinkButton cmdCancel;
		protected System.Web.UI.WebControls.HyperLink cmdHelp;
		protected System.Web.UI.WebControls.Label lblInfo;
		private string _key;
		private string MyFileName = "Help.ascx";
		protected override void OnLoad(EventArgs e)
		{
			base.OnLoad(e);

			cmdCancel.Click += new EventHandler(cmdCancel_Click);

			string FriendlyName = "";
			ModuleController objModules = new ModuleController();
			ModuleInfo objModule = objModules.GetModule(ModuleId, TabId, false);
			if (objModule != null)
			{
				FriendlyName = objModule.DesktopModule.FriendlyName;
			}
			int ModuleControlId = Null.NullInteger;
			if (Request.QueryString["ctlid"] != null)
			{
				ModuleControlId = Int32.Parse(Request.QueryString["ctlid"]);
			}
			ModuleControlInfo objModuleControl = ModuleControlController.GetModuleControl(ModuleControlId);
			if (objModuleControl != null)
			{
				string FileName = Path.GetFileName(objModuleControl.ControlSrc);
				string LocalResourceFile = objModuleControl.ControlSrc.Replace(FileName, Services.Localization.Localization.LocalResourceDirectory + "/" + FileName);
				if (!String.IsNullOrEmpty(Services.Localization.Localization.GetString(ModuleActionType.HelpText, LocalResourceFile)))
				{
					lblHelp.Text = Services.Localization.Localization.GetString(ModuleActionType.HelpText, LocalResourceFile);
				}
				else
				{
					lblHelp.Text = Services.Localization.Localization.GetString("lblHelp.Text", Services.Localization.Localization.GetResourceFile(this, MyFileName));
				}
				_key = objModuleControl.ControlKey;
				string helpUrl = Common.Globals.GetOnLineHelp(objModuleControl.HelpURL, ModuleConfiguration);
				if (!string.IsNullOrEmpty(helpUrl))
				{
					cmdHelp.NavigateUrl = Common.Globals.FormatHelpUrl(helpUrl, PortalSettings, FriendlyName);
					cmdHelp.Visible = true;
				}
				else
				{
					cmdHelp.Visible = false;
				}
				if (UserInfo.IsSuperUser)
				{
					string strInfo = Services.Localization.Localization.GetString("lblInfo.Text", Services.Localization.Localization.GetResourceFile(this, MyFileName));
					strInfo = strInfo.Replace("[CONTROL]", objModuleControl.ControlKey);
					strInfo = strInfo.Replace("[SRC]", objModuleControl.ControlSrc);
					ModuleDefinitionInfo objModuleDefinition = ModuleDefinitionController.GetModuleDefinitionByID(objModuleControl.ModuleDefID);
					if (objModuleDefinition != null)
					{
						strInfo = strInfo.Replace("[DEFINITION]", objModuleDefinition.FriendlyName);
						DesktopModuleInfo objDesktopModule = DesktopModuleController.GetDesktopModule(objModuleDefinition.DesktopModuleID, PortalId);
						if (objDesktopModule != null)
						{
							PackageInfo objPackage = PackageController.GetPackage(objDesktopModule.PackageID);
							if (objPackage != null)
							{
								strInfo = strInfo.Replace("[ORGANIZATION]", objPackage.Organization);
								strInfo = strInfo.Replace("[OWNER]", objPackage.Owner);
								strInfo = strInfo.Replace("[EMAIL]", objPackage.Email);
								strInfo = strInfo.Replace("[URL]", objPackage.Url);
								strInfo = strInfo.Replace("[MODULE]", objPackage.Name);
								strInfo = strInfo.Replace("[VERSION]", objPackage.Version.ToString());
							}
						}
					}
					lblInfo.Text = Server.HtmlDecode(strInfo);
				}
			}
			if (Page.IsPostBack == false)
			{
				if (Request.UrlReferrer != null)
				{
					ViewState["UrlReferrer"] = Convert.ToString(Request.UrlReferrer);
				}
				else
				{
					ViewState["UrlReferrer"] = "";
				}
			}
		}

		protected void cmdCancel_Click(System.Object sender, System.EventArgs e)
		{
			try
			{
				Response.Redirect(Convert.ToString(ViewState["UrlReferrer"]), true);
			}
			catch (Exception exc)
			{
				Services.Exceptions.Exceptions.ProcessModuleLoadException(this, exc);
			}
		}
	}
}
